<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use App\Components\FileAdapter;
use App\Service\IndexService;
use App\Service\Instance\JwtInstance;
use App\Service\UserService;
use Hyperf\Context\Context;
use Hyperf\Contract\ConfigInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Elasticsearch\ClientBuilderFactory;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;
use League\Flysystem\Config;

#[AutoController]
class IndexController extends AbstractController
{

    /**
     * @var IndexService
     */
    #[Inject]
    public $indexService;

    #[Inject]
    public UserService $userService;

    public function testEs(){
        // 如果在协程环境下创建，则会自动使用协程版的 Handler，非协程环境下无改变
        $builder = $this->container->get(ClientBuilderFactory::class)->create();
        $client = $builder->setHosts([env('ELASTICSEARCH_HOST')])->build();
        $info = $client->info();
        $params = [
            'index' => 'question',
            'type' => '_doc',
            'id' => '2'
        ];
        $qInfo = $client->get($params);
        $params = [
            'index' => 'question',
            'type' => '_doc',
        ];
        $params['body']['query']['match']['content'] = 'hello';
        $matchRes = $client->search($params);
        return [
            'info' => $info,
            'qInfo' => $qInfo,
            'matchInfo' => $matchRes,
        ];
    }

    /**
     * 依赖注入引入封装的类
     * @param FileAdapter $fileAdapter
     * @return array
     */
    public function fileQiniu2(FileAdapter $fileAdapter){
        $path = "content/2.txt";
        $content = "hello qiniu 撒dasds";
        $fileAdapter->write($path,$content);
        $url = $fileAdapter->privateDownloadUrl($path);
        return [
            'url' => $url,
            'content' => $fileAdapter->read($url),
        ];
    }

    public function fileQiniu(){
        $path = "content/1.txt";
        $content = "hello qiniu";
        // 上传至七牛
        $container = ApplicationContext::getContainer();
        $options = $container->get(ConfigInterface::class)->get('file');

        $filesystemFactory = $container->get(FilesystemFactory::class);
        $adapter = $filesystemFactory->getAdapter($options, 'qiniu');
        // 上传内容到七牛
        $adapter->write($path, $content, new Config());
        // 获取私有链接
        $url = $adapter->privateDownloadUrl($path);
        return [
            'url' => $url,
            'content' => $adapter->read($url),
        ];
    }

    public function mulIndex(){
        $uids = [2, 3];
        return $this->userService->getMultiUserInfosFromCache($uids);
    }

    public function testCacheLogin(){
        $id = $this->request->input('id');
        return $this->userService->getUserInfoFromCache($id);
    }

    public function info11(){
        $res = $this->indexService->info3();
        return $this->response->success($res);
    }
    public function info22(){
        $res = $this->indexService->info4();
        return $this->response->success($res);
    }


    public function mail(){
        return $this->response->success($this->indexService->mail());
    }
    public function mail2(){
        return $this->response->success($this->indexService->mail2());
    }

    public function mail3(){
        return $this->response->success($this->indexService->mail3());
    }

    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function info(){
        $id = (int) $this->request->input('id', 0);
        if ($id > 0) {
            return $this->response->success(['info' => 'data info']);
        } else {
            return $this->response->fail(500, 'id无效');
        }
    }

    public function info2(){
        try {
            return $this->response->success($this->indexService->info($id));
        } catch (\Throwable $e) {
            return $this->response->fail(500, $e->getMessage());
        }
    }
    public function info3(){
        $id = (int) $this->request->input('id', 0);
        return $this->response->success($this->indexService->info2($id));
    }

    public function info4(){
        var_dump(convert_size(100));
    }

    public $a;
    public function info5()
    {
        $a = $this->request->input('a');
        Context::set('a', $a);
//        if ($a) {
//            $this->a = $a;
//        }


        return [
            'co_is' => Coroutine::inCoroutine(), // 判断当前是否在协程内
            'co_id' => Coroutine::id(), // 获取当前协程 id
//            'a' => $this->a,
            'a' => Context::get('a')
        ];
    }

    public function info6()
    {
        $id = $this->request->input('id');

        $user = [
            'id' => $id,
            'name' => $id . '_' . 'name',
        ];
        $instance = JwtInstance::instance();
        $instance->encode($user);

        return [
            'id' => $instance->id,
            'user' => $instance->user,
        ];
    }

    public function info7()
    {
        $result = Db::select('SELECT * FROM email_code;');
        return $result;
    }

    public function redis(){
        $container = ApplicationContext::getContainer();
        $redis = $container->get(\Hyperf\Redis\Redis::class);
        $redis->set('a',2);
    }
}
